﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace EncorePlugin
{
    public class EncoreService
    {
        private static EncoreService m_encoreService = null;

        private EncoreService() { }

        public static EncoreService GetInstance()
        {
            if (m_encoreService == null)
            {
                m_encoreService = new EncoreService();
            }

            return m_encoreService;
        }

        public List<EncorePlaylistItem> GetPlaylist()
        {
            string line;
            List<EncorePlaylistItem> list = new List<EncorePlaylistItem>();

            StreamReader file = new StreamReader("c:\\program files\\EnCore 7.62\\tempList.txt", Encoding.GetEncoding("windows-1251"));
            while ((line = file.ReadLine()) != null)
            {
                EncorePlaylistItem item = EncorePlaylistItem.Parse(line);
                if (item != null)
                {
                    list.Add(item);
                }
            }

            file.Close();

            return list;
        }
    }

    public class EncorePlaylistItem
    {
        public EncorePlaylistItem()
        {
        }

        public EncorePlaylistItem(int id, int extId, string name, string artist, bool hasBackSinging, string path)
        {
            Id = id;
            ExtId = extId;
            Name = name;
            Artist = artist;
            HasBackSinging = hasBackSinging;
            Path = path;
        }

        public int Id { get; set; }

        public int ExtId { get; set; }

        public string Name { get; set; }

        public string Artist { get; set; }

        public bool HasBackSinging { get; set; }

        public string Path { get; set; }

        public static EncorePlaylistItem Parse(string str)
        {
            if (!string.IsNullOrEmpty(str))
            {
                string[] strArray = str.Split('°');
                if (strArray != null && strArray.Length > 0)
                {
                    EncorePlaylistItem item = new EncorePlaylistItem();
                    item.Id = ToInt(strArray[0], 0);
                    if (strArray.Length > 1)
                    {
                        item.Name = strArray[1];
                        if (strArray.Length > 2)
                        {
                            item.Artist = strArray[2];
                            if (strArray.Length > 3)
                            {
                                item.HasBackSinging = strArray[3].Equals('•');
                                if (strArray.Length > 4)
                                {
                                    item.ExtId = ToInt(strArray[4], 0);
                                    if (strArray.Length > 5)
                                    {
                                        item.Path = strArray[5];
                                    }
                                }
                            }
                        }
                    }
                    return item;
                }
            }

            return null;
        }

        private static int ToInt(string str, int def)
        {
            try
            {
                return int.Parse(str);
            }
            catch
            {
                return def;
            }
        }
    }
}
